package com.one.service.impl;

import com.one.mapper.LandMapper;
import com.one.mapper.RateDailyRecordMapper;
import com.one.mapper.StoreMapper;
import com.one.pojo.Land;
import com.one.pojo.OneUser;
import com.one.pojo.RateDailyRecord;
import com.one.pojo.Store;
import com.one.service.LandService;
import com.one.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description
 * @Author semstouch
 * @Date 2017/4/7
 **/
@Service
public class LandServiceImpl implements LandService {
    @Resource
    private LandMapper landMapper;
    @Resource
    private StoreMapper storeMapper;
    @Resource
    private RateDailyRecordMapper rateDailyRecordMapper;

    @Override
    public boolean add(Land land) {
        land.setId(StringUtils.getUUID());
        return landMapper.insertSelective(land)>0?true:false;
    }

    @Override
    public Land getEggsFromLand(String userId, String landId) {
        Store store= storeMapper.findByUserId(userId);
        Land land =landMapper.findByChickenfarmIdAndLandId(userId,landId);
        if(land.getCollectFlag().equals("1")){
            return null;
        }
        BigDecimal eggNum=store.getEggsNum().add(land.getEggsIncome());
        store.setEggsNum(eggNum);
        land.setCollectFlag("1");

        storeMapper.updateByPrimaryKey(store);
        landMapper.updateByPrimaryKeySelective(land);
        return land;
    }

    @Override
    public Land putChickenToStore(String userId, String landId) {
        Store store= storeMapper.findByUserId(userId);
        Land land =landMapper.findByChickenfarmIdAndLandId(userId,landId);
        BigDecimal landChicksNum=new BigDecimal(land.getChicksNum()).subtract(land.getMinNum());
        BigDecimal chicksNum=store.getChicksNum().add(landChicksNum);
        store.setChicksNum(chicksNum);
        land.setChicksNum(land.getMinNum().intValue());

        storeMapper.updateByPrimaryKey(store);
        landMapper.updateByPrimaryKeySelective(land);
        return land;
    }

    @Override
    public boolean update(Land land) {
        return landMapper.updateByPrimaryKeySelective(land)>0?true:false;
    }

    @Override
    public List<Land> findAll(String chickenfarmId) {
        return landMapper.findByChickenfarmId(chickenfarmId);
    }

    @Override
    public Land findById(String userId, String landId) {
        return landMapper.findByChickenfarmIdAndLandId(userId,landId);
    }

    @Override
    public Land update(Land land, Store store) {
        landMapper.updateByPrimaryKeySelective(land);
        storeMapper.updateByPrimaryKey(store);
        return null;
    }

    @Override
    public boolean dailyLand(OneUser oneUser,BigDecimal pubRate,BigDecimal ratio) {
        List<Land> landList = landMapper.findByChickenfarmId(oneUser.getFarmId());
        BigDecimal dogRatio = new BigDecimal(0.05);
        BigDecimal fenceRatio = new BigDecimal(0.1);
        BigDecimal dogRate = new BigDecimal(0);
        BigDecimal fenceRate = new BigDecimal(4);


        if(oneUser.getDogLevel()==null||oneUser.getDogLevel().equals("")){
            oneUser.setDogLevel("0");
        }
        if(oneUser.getFenceLevel()==null||oneUser.getFenceLevel().equals("")){
            oneUser.setFenceLevel("0");
        }

        fenceRate=fenceRate.subtract(new BigDecimal(oneUser.getFenceLevel()));
        fenceRate=fenceRate.multiply(fenceRatio);
        dogRate = new BigDecimal(oneUser.getDogLevel()).multiply(dogRatio);


        BigDecimal personRate = pubRate.add(dogRate);
        personRate= personRate.subtract(fenceRate);
        BigDecimal eggIncome =new BigDecimal(0);


        if(landList!=null){
            for(int i=0;i<landList.size();i++){
                Land  land =landList.get(i);
                BigDecimal eggNum = land.getEggsIncome();
                BigDecimal dailyEgg= new BigDecimal(land.getChicksNum()).multiply(personRate).multiply(new BigDecimal(0.01));
                BigDecimal totalIncome= land.getTotalIncome();
                if(land.getCollectFlag().equals("0")) {
                    eggNum = eggNum.add(dailyEgg);
                }else{
                    eggNum= dailyEgg;
                }
                eggIncome=eggIncome.add(dailyEgg);
                totalIncome= totalIncome.add(dailyEgg);
                land.setEggsIncome(eggNum);
                land.setTotalIncome(totalIncome);
                land.setCollectFlag("0");
                landMapper.updateByPrimaryKeySelective(land);
            }
        }
        RateDailyRecord rateDailyRecord = new RateDailyRecord();
        rateDailyRecord.setId(StringUtils.getUUID());
        rateDailyRecord.setUserId(oneUser.getUserId());
        rateDailyRecord.setDaily(StringUtils.getStringYesterday());
        rateDailyRecord.setPubRate(pubRate);
        rateDailyRecord.setRatio(ratio);
        rateDailyRecord.setDogRate(dogRate);
        rateDailyRecord.setDogRatio(dogRatio);
        rateDailyRecord.setEggIncome(eggIncome);
        rateDailyRecord.setPersonRate(personRate);
        rateDailyRecord.setFenceRatio(fenceRatio);
        rateDailyRecord.setFenceRate(fenceRate.multiply(new BigDecimal(-1)));
        rateDailyRecord.setDogLevel(Integer.parseInt(oneUser.getDogLevel()));
        rateDailyRecord.setFenceLevel(Integer.parseInt(oneUser.getFenceLevel()));
        rateDailyRecord.setCreateTime(StringUtils.getNowTime());
        rateDailyRecordMapper.insertSelective(rateDailyRecord);
        return false;
    }
}
